using Godot;
using System;

public partial class AreaMagic : Area2D
{
	private bool haveEntry = false;
	private Card _targeCard;
	private int  grade;
	private Godot.Collections.Dictionary data;
	[Signal]
	public delegate void useMagicSignalEventHandler(Card p,int grade);
	[Export]
	public Sprite2D parentNode { get; set; }
	[Export]
	public Sprite2D parentNode2 { get; set; }
	[Export]
	public Sprite2D parentNode3 { get; set; }
	public override void _Ready()
	{
		using FileAccess file = FileAccess.Open("res://data.json", FileAccess.ModeFlags.Read);
		string jsonText = file.GetAsText();
		Json json = new Json();
		Error error = json.Parse(jsonText);
		data = json.Data.AsGodotDictionary();
		MouseEntered += OnBodyEntered;
		MouseExited += OnBodyExited;
	}
	public void OnBodyEntered()
	{
		haveEntry = true;
	}
	public void OnBodyExited()
	{
		haveEntry = false;

	}
	public override void _Input(InputEvent @event)
	{
		if (@event is InputEventMouseButton mouseEvent &&
		mouseEvent.ButtonIndex == MouseButton.Left)
		{
			if (mouseEvent.Pressed)
			{
				if (haveEntry)
				{
					EmitSignal(SignalName.useMagicSignal, _targeCard,grade);
					parentNode.Visible = false;
					parentNode3.Visible = false;
					parentNode2.Visible = false;
				}
			}
		}
	}
	public void showCardSignalHandler(int grade, Card node,int atkTemp,int defTemp)
	{
		
		this.grade = grade;
		_targeCard = node;
		if (grade == 2)
		{
			parentNode.Texture = GD.Load<Texture2D>("res://photo/card/high/坤之高兴.png");
			parentNode.Visible = true;
		}else if(grade == 4){
			parentNode.Texture = GD.Load<Texture2D>("res://photo/card/high/可汗快乐轮.png");
			parentNode.Visible = true;
		}else if(grade == 6){
			parentNode.Texture = GD.Load<Texture2D>("res://photo/card/high/观星.png");
			parentNode.Visible = true;
		}else if(grade == 8){
			parentNode.Texture = GD.Load<Texture2D>("res://photo/card/high/赌怪.png");
			parentNode.Visible = true;
		}else if(grade == 10){
			parentNode.Texture = GD.Load<Texture2D>("res://photo/card/high/飞升.png");
			parentNode.Visible = true;
		}else{
			
		}
	}
}
